<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\trade;

use Medoo\Medoo;

class OrderParent extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'order_parent';


    //pay_way的值
    const PAY_WAY_ALIPAY_APP = 'alipay';//支付宝APP支付
    const PAY_WAY_WXPAY_APP = 'wechat';//微信APP支付


    public static $payWayDesc = [
        self::PAY_WAY_ALIPAY_APP => '支付宝APP支付',
        self::PAY_WAY_WXPAY_APP => '微信APP支付',
    ];

    const IS_CAN_PAY_YES = 1; //主单可以支付，子单也可以支付
    const IS_CAN_PAY_NO = 0;//主单不可支付


    public static function insertParentOrder($data)
    {
        return self::insert($data)->rowCount();
    }

    public static function getOrderParentInfoFromMaster($orderParentNo)
    {

        $info = self::select_master("*", Medoo::raw(' where order_parent_no=:orderParentNo for update', [':orderParentNo' => $orderParentNo]));

        $info = array_shift($info);


        if (empty($info)) {
            return false;
        }

        return $info;
    }

    public static function getOrderParentInfo($orderParentNo)
    {
        $info = self::get("*", ['order_parent_no' => $orderParentNo]);

        if (empty($info)) {
            return [];
        }

        return $info;
    }

    public static function changeCanPayToNo($orderParentNo)
    {
        return self::update(['is_can_pay' => 0], ['order_parent_no' => $orderParentNo, 'is_can_pay' => self::IS_CAN_PAY_YES]);
    }

    public static function getOrderInfoByOrderParentNo($parentOrder)
    {
        return self::get('*', ['order_parent_no' => $parentOrder]);
    }

    public static function updateData($data, $where)
    {
        return self::update(
            $data,
            $where
        );
    }
}